package com.lp.yupao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lp.yupao.common.BaseResponse;
import com.lp.yupao.common.ErrorCode;
import com.lp.yupao.exception.BusinessException;
import com.lp.yupao.model.domain.User;
import com.lp.yupao.model.domain.request.UserRegisterRequest;
import com.lp.yupao.service.UserService;
import com.lp.yupao.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册接口
     * @param userRegisterRequest 用户注册请求体
     * @return 用户id
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAllBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long userId = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(userId);
    }

    /**
     * 用户登录接口
     * @param userLoginRequest 用户登录的请求体
     * @param request 请求头
     * @return user信息
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserRegisterRequest userLoginRequest, HttpServletRequest request){
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAllBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

    /**
     * 注销
     *
     * @param request 请求
     * @return 是否注销成功，成功返回1
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        Integer result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户信息(通过userName)
     *
     * @param userName 用户名
     * @param request 请求头
     * @return 用户信息
     */
    @GetMapping("/select")
    public BaseResponse<List<User>> selectUserByUserName(String userName, HttpServletRequest request){
        List<User> userList = userService.selectUserByUserName(userName, request);
        return ResultUtils.success(userList);
    }

    /**
     * 获取用户登录态
     * @param request 请求头
     * @return  user对象
     */
    @GetMapping("/current")
    private BaseResponse<User> getCurrentUser(HttpServletRequest request){
        User currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(currentUser);
    }

    /**
     * 通过id删除用户信息
     *
     * @param userId   用户id
     * @return 是否删除成功 成功为 1
     */
    @PostMapping("/delete")
    private BaseResponse<Integer> deleteUserById(Long userId,HttpServletRequest request){
        Integer result = userService.deleteUserById(userId,request);
        return ResultUtils.success(result);
    }

    /**
     * 根据标签查用户
     *
     * @param tagNameList 标签列表
     * @return
     */
    @GetMapping("/tags")
    private BaseResponse<List> searchTagNameList(@RequestParam(required = false) List<String> tagNameList){
        //判断参数是否为空
        if (CollectionUtils.isEmpty(tagNameList)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userService.searchUserByTagsBySql(tagNameList));
    }

    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @param request 请求
     * @return 更新结果
     */
    @PostMapping("/updateUser")
    private BaseResponse<Integer> updateUser(@RequestBody User user,HttpServletRequest request){
        // 判断参数是否为空
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取登录用户
        User loginUser = userService.getLoginUser(request);
        // 更新数据
        return ResultUtils.success(userService.updateUser(user, loginUser));
    }

    /**
     * 分页获取用户数据
     *
     * @param pageNum 页数
     * @param pageSize 每页大小
     * @param request 请求
     * @return 用户列表
     */
    @GetMapping("/recommend")
    private BaseResponse<Page> recommendUsers(long pageNum,long pageSize,HttpServletRequest request){
        Page<User> userList = userService.recommendUsers(pageNum,pageSize,request);
        return ResultUtils.success(userList);
    }

    /**
     * 随机匹配用户
     *
     * @param num 匹配的用户数
     * @param request 请求
     * @return 用户列表
     */
    @GetMapping("/match")
    private BaseResponse<List<User>> matchUserList(int num,HttpServletRequest request){
        if (num <=0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        List<User> userList = userService.matchUserList(num,loginUser);
        return ResultUtils.success(userList);
    }
}
